/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.platform.web.controller.agent;


import cn.dev33.satoken.annotation.SaCheckLogin;
import com.ijpay.alipay.AliPayApi;
import com.medusa.gruul.common.core.annotation.EscapeLogin;
import com.medusa.gruul.common.core.util.PageUtils;
import com.medusa.gruul.common.core.util.Result;
import com.medusa.gruul.platform.model.dto.agent.AgentBalanceRechargeDto;
import com.medusa.gruul.platform.model.vo.BalanceRechargeVo;
import com.medusa.gruul.platform.model.vo.agent.BalanceDetailsVo;
import com.medusa.gruul.platform.service.IAgentRechargeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.io.IOException;

/**
 * <p>
 * 代理商充值记录表 前端控制器
 * </p>
 *
 * @author whh
 * @since 2020-10-04
 */
@RestController
@RequestMapping("/agent/console/balance")
@SaCheckLogin
@Api(tags = "代理后台相关")
public class AgentRechargeController {

    @Autowired
    private IAgentRechargeService agentRechargeService;

    @PostMapping("/recharge")
    @EscapeLogin
    @ApiOperation(value = "代理商为商户充值")
    public Result<BalanceRechargeVo> agentBalanceRecharge(@RequestBody @Validated AgentBalanceRechargeDto dto) {
        BalanceRechargeVo vo = agentRechargeService.agentBalanceRecharge(dto);
        return Result.ok(vo);
    }

    @GetMapping("/recharge/{orderId}")
    @EscapeLogin
    @ApiOperation(value = "查询指定订单是否支付成功,支付成功返回date=true")
    public Result<Boolean> agentBalanceRechargePayIfOk(@PathVariable(name = "orderId") Long orderId) {
        Boolean flag = agentRechargeService.agentBalanceRechargePayIfOk(orderId);
        return Result.ok(flag);
    }


    @PostMapping("/notify/wx")
    @EscapeLogin
    @ApiOperation(value = "代理为商户余额充值微信回调接口", hidden = true)
    public String rechargeNotifyWx(HttpServletRequest request) throws IOException {
        String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
        return agentRechargeService.rechargeNotifyWx(xmlResult);
    }


    @PostMapping(value = "/notify/alipay")
    @EscapeLogin
    @ApiOperation(value = "代理为商户余额充值阿里回调接口", hidden = true)
    public String notifyAlipay(HttpServletRequest request) {
        return agentRechargeService.rechargeNotifyAlipay(AliPayApi.toMap(request));
    }


    @GetMapping(value = "detail")
    @EscapeLogin
    @ApiOperation(value = "查询代理的充值明细", tags = "代理后台相关")
    public Result<PageUtils<BalanceDetailsVo>> balanceDetails(@ApiParam(value = "指定页数", required = true) @RequestParam Integer page,
                                                              @ApiParam(value = "数据条数", required = true) @RequestParam Integer size,
                                                              @ApiParam(value = "开始时间") @RequestParam(required = false) String startTime,
                                                              @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime,
                                                              @ApiParam(value = "订单号或者账号") @RequestParam(required = false) String search,
                                                              @ApiParam(value = "订单状态 1:充值中 2:充值成功") @Valid @Max(value = 2) @Min(value = 1) @RequestParam String status
    ) {
        PageUtils<BalanceDetailsVo> result = agentRechargeService.balanceDetails(page, size, startTime, endTime, search, status);
        return Result.ok(result);
    }
}
